<?php

namespace addons\groupon\controller;

use addons\groupon\exception\Exception;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Border;


class Goods extends Base
{

    protected $noNeedLogin = ['index', 'detail', 'lists', 'activity', 'seckillList', 'grouponList', 'store'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        
    }

    public function detail()
    {
        $id = $this->request->get('id');
        $detail = \addons\groupon\model\Goods::getGoodsDetail($id);
        
        // 记录足记
        \addons\groupon\model\UserView::addView($detail);

        $sku_price = $detail['sku_price'];      // 处理过的规格
        // tp bug json_encode 或者 toArray 的时候 sku_price 会重新查询数据库，导致被处理过的规格又还原回去了
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;

        $this->success('商品详情', $detail);
    }

    public function lists()
    {
        $params = $this->request->get();
        $data = \addons\groupon\model\Goods::getGoodsList($params);

        $this->success('商品列表', $data);

    }



    public function favorite()
    {
        $params = $this->request->post();
        $result = \addons\groupon\model\UserFavorite::edit($params);
        $this->success($result ? '收藏成功' : '取消收藏', $result);
    }

    public function favoriteList()
    {
        $data = \addons\groupon\model\UserFavorite::getGoodsList();
        $this->success('商品收藏列表', $data);
    }


    public function viewDelete()
    {
        $params = $this->request->post();
        $result = \addons\groupon\model\UserView::del($params);
        $this->success('删除成功', $result);
    }


    public function viewList()
    {
        $data = \addons\groupon\model\UserView::getGoodsList();
        $this->success('商品浏览列表', $data);
    }



}
